﻿
Imports System.Net.Sockets
Imports osi.root.connector
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils
Imports osi.service.device
Imports osi.service.commander

Partial Public Class powerpoint
    Public Event new_device_inserted() Implements new_device_event_device_pool.new_device_inserted
    Public Event closing() Implements idevice_pool.closing

    Public Function max_count() As UInt32 Implements idevice_pool.max_count
        Return max_connected()
    End Function

    Public Function identity() As String Implements idevice_pool.identity
        If is_outgoing Then
            Return strcat("outgoing@", host_or_ip, ":", port)
        Else
            Return strcat("incoming@", port)
        End If
    End Function

    Public Function total_count() As UInt32 Implements idevice_pool.total_count
        Return pool.total_count()
    End Function

    Public Function free_count() As UInt32 Implements idevice_pool.free_count
        Return pool.free_count()
    End Function

    Public Function expired() As Boolean Implements idevice_pool.expired
        Return pool.expired()
    End Function

    Public Sub close() Implements idevice_pool.close
        pool.close()
    End Sub

    Public Function [get](ByRef o As TcpClient) As Boolean Implements isync_device_pool(Of TcpClient).sync_get
        Dim c As connection = Nothing
        If pool.sync_get(c) AndAlso assert(Not c Is Nothing) Then
            o = c.client()
            Return True
        Else
            Return False
        End If
    End Function

    Public Function [get](ByRef o As flow) As Boolean Implements isync_device_pool(Of flow).sync_get
        Dim c As TcpClient = Nothing
        Return [get](c) AndAlso
               assert(Not c Is Nothing) AndAlso
               as_flow(c, o)
    End Function

    Public Function release(ByVal c As TcpClient) As Boolean Implements idevice_pool(Of TcpClient).release
        If c Is Nothing Then
            Return False
        Else
            Return pool.release(New connection(c))
        End If
    End Function

    Public Function release(ByVal f As flow) As Boolean Implements idevice_pool(Of flow).release
        Dim o As TcpClient = Nothing
        Return as_tcp_client(f, o) AndAlso
               release(o)
    End Function
End Class
